/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.query;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.sql.Timestamp;

/**
 * @author bduphoff
 * 
 *         TODO To change the template for this generated type comment go to
 *         Window - Preferences - Java - Code Style - Code Templates
 */
public class TimestampRange {
	public Timestamp startTS = null;

	public Timestamp endTS = null;

	public static TimestampRange generate(String startTS, String endTS)
			throws Exception {
		TimestampRange tsr = new TimestampRange();
		GregorianCalendar cal = null;
		long startMS = -1;
		try {
			startMS = Long.parseLong(startTS);
			tsr.startTS = new Timestamp(startMS);
			cal = new GregorianCalendar();
			cal.setTimeInMillis(startMS);
		} catch (NumberFormatException nfe) {

		}
		if (startMS == -1) {
			String[] timestampTokens = startTS.split(" ");
			String[] tokens = timestampTokens[0].split("-");
			cal = new GregorianCalendar(Integer.parseInt(tokens[0]), Integer
					.parseInt(tokens[1]) - 1, Integer.parseInt(tokens[2]));
			if (timestampTokens.length == 2) {
				// There is a start times, should be 24HH:MM:SS
				String[] tokens2 = timestampTokens[1].split(":");
				cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(tokens2[0]));
				cal.set(Calendar.MINUTE, Integer.parseInt(tokens2[1]));
				int idx = tokens2[2].indexOf(".");
				if (idx == -1)
					cal.set(Calendar.SECOND, Integer.parseInt(tokens2[2]));
				else
					cal.set(Calendar.SECOND, Integer.parseInt(tokens2[2]
							.substring(0, idx)));
				cal.set(Calendar.MILLISECOND, 0);
			} else {

				cal.set(Calendar.HOUR_OF_DAY, 0);
				cal.set(Calendar.MINUTE, 0);
				cal.set(Calendar.SECOND, 0);
				cal.set(Calendar.MILLISECOND, 0);

			}
			tsr.startTS = new Timestamp(cal.getTimeInMillis());
		}

		if (endTS != null) {
			long endMS = -1;
			try {
				endMS = Long.parseLong(endTS);
				tsr.endTS = new Timestamp(endMS);
			} catch (NumberFormatException nfe) {

			}
			if (endMS == -1) {
				String[] timestampTokens = endTS.split(" ");
				String[] tokens = timestampTokens[0].split("-");
				cal = new GregorianCalendar(Integer.parseInt(tokens[0]),
						Integer.parseInt(tokens[1]) - 1, Integer
								.parseInt(tokens[2]));
				if (timestampTokens.length == 2) {
					// There is a start times, should be 24HH:MM:SS
					String[] tokens2 = timestampTokens[1].split(":");
					cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(tokens2[0]));
					cal.set(Calendar.MINUTE, Integer.parseInt(tokens2[1]));
					int idx = tokens2[2].indexOf(".");
					if (idx == -1)
						cal.set(Calendar.SECOND, Integer.parseInt(tokens2[2]));
					else
						cal.set(Calendar.SECOND, Integer.parseInt(tokens2[2]
								.substring(0, idx)));

					cal.set(Calendar.MILLISECOND, 999);
				} else {

					cal.set(Calendar.HOUR_OF_DAY, 23);
					cal.set(Calendar.MINUTE, 59);
					cal.set(Calendar.SECOND, 59);
					cal.set(Calendar.MILLISECOND, 999);

				}
				tsr.endTS = new Timestamp(cal.getTimeInMillis());
			}
		} else { // There is no end TS, set to the end of the day
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MILLISECOND, 0);
			cal.add(Calendar.DAY_OF_MONTH, 1);
			cal.add(Calendar.MILLISECOND, -1);
			tsr.endTS = new Timestamp(cal.getTimeInMillis());
		}
		return tsr;
	}

	public String toXML() {
		return "<Timestamp start=\"" + startTS + "\" end=\"" + endTS + "\"/>";
	}

}